package com.mediastory.spsserver.adCategory.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.mediastory.spsserver.adCategory.dao.AdCategoryDao;
import com.mediastory.spsserver.adCategory.domain.AdCategory;
import com.mediastory.spsserver.adCategory.service.AdCategorySvc;
import com.mediastory.spsserver.content.domain.Content;

@Service
@Transactional
public class AdCategorySvcImpl implements AdCategorySvc {

	@Autowired
	AdCategoryDao adCategoryDao;
	
	public AdCategorySvcImpl() {
		// TODO Auto-generated constructor stub
	}

	@Override
	public List<AdCategory> findbyList(Map<String, Object> condition)
			throws Exception {
		// TODO Auto-generated method stub
		return (List<AdCategory>)(adCategoryDao.SelectList(condition));
	}

	@Override
	public AdCategory findby(Integer id) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public AdCategory add(AdCategory AdCategory) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public AdCategory modify(AdCategory AdCategory) throws Exception {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int remove(Integer id) throws Exception {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public List<Content> findbyListInCont(Map<String, Object> condition) throws Exception {
		// TODO Auto-generated method stub
		return (List<Content>)(adCategoryDao.SelectListInCont(condition));
	}
	
	public Map<String, Object> findbyListPagingInCont(Map<String, Object> condition)
			throws Exception {
		// TODO Auto-generated method stub
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("contents", (List<Content>) (adCategoryDao.SelectListInCont(condition)));
		resultMap.put("contentsCnt", (Integer) (adCategoryDao.SelectListInContTotalCnt(condition)));
		
		return resultMap;
	}

	@Override
	public List<Content> findbyListOutCont(Map<String, Object> condition)
			throws Exception {
		// TODO Auto-generated method stub
		return (List<Content>)(adCategoryDao.SelectListOutCont(condition));
	}

	@Override
	public Integer addDispOrder(Map<String, Object> condition) throws Exception {
		// TODO Auto-generated method stub
		int result = 0;
		Integer adCategoryIdx = (Integer)condition.get("adCategoryIdx");
		int[] dispOrderList = (int[])condition.get("dispOrderList");
		
		//기존에 등록된 목록 삭제
		adCategoryDao.deleteDispOrder(condition);
		
		//등록
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("adCategoryIdx", adCategoryIdx);
		
		for (int i=0;i<dispOrderList.length;i++) {
			int adIdx = dispOrderList[i];
			map.put("adIdx", adIdx);
			map.put("dispOrder", i);
			
			result = adCategoryDao.insertDispOrder(map);
		}
		return result;
	}

	@Override
	public Integer removeDispOrder(Map<String, Object> condition) throws Exception {
		// TODO Auto-generated method stub
		return (Integer)(adCategoryDao.deleteDispOrder(condition));
	}

}